package com.myebills.ebpsfrontend.cust.controller;

import com.checkfree.isolutions.security.Cipher;
import javax.servlet.ServletException;

public class EbppxCipher {

    /**
     * The recommended value for the cipher algorithm,
     * which is "3DES/PCBC/PKCS5".
     * <b><em>Note:</em></b>
     * For the recommended algorithm,
     * a 24-character key is required.
     */
    public static final String PREFERRED_CIPHER_ALGORITHM = "3DES/PCBC/PKCS5";

    /**
     * The required length of the text key if the preferred cipher algorithm
     * is used.
     */
    public static final int PREFERRED_CIPHER_KEY_LENGTH = 24;

    /**
     * Encrypt some text.
     *
     * @param   method The desired cipher algorithm.
     *          The recommended value is "3DES/PCBC/PKCS5".
     *
     * @param   key The secret encryption key.
     *          For the recommended cipher method,
     *          a 24-character key is required.
     *
     * @param   decrypted The plain-text to be encrypted
     *
     * @param   option Pass <b>1</b> to include an integrity header in the
     *          result (recommended);
     *          pass <b>0</b> to omit the header.
     *
     * @return  The encrypted result.
     */
    public static String encryptFromString(
        String method,
        String key,
        String decrypted,
        int option
    ) throws ServletException {
        try {
            System.out.println("METHOD  = ["+ method +"]");
            System.out.println("KEY     = ["+ key +"]");
            System.out.println("INPUT   = ["+ decrypted +"]");
            System.out.println("OPTION  = ["+ option +"]");
            //String s = Cipher.encryptFromString(method, key, decrypted, 0);
            String s = EncryptionHelper.encrypt(decrypted,key);
            System.out.println("ENCRYPT = ["+ s +"]");
            return s;
        } catch (Throwable t) {
            throw new ServletException(
                    "Error encountered in EbppxCipher.encryptFromString: " +
                        t.toString(),
                    t
                    );
        }
    }

    /**
     * Encrypt a character array.
     *
     * @param   method The desired cipher algorithm.
     *          The recommended value is "3DES/PCBC/PKCS5".
     *
     * @param   key The secret encryption key.
     *          For the recommended cipher method,
     *          a 24-character key is required.
     *
     * @param   decrypted The plain-text to be encrypted
     *
     * @param   option Pass <b>1</b> to include an integrity header in the
     *          result (recommended);
     *          pass <b>0</b> to omit the header.
     *
     * @return  The encrypted result.
     */
    /*public static char[] encryptFromCharArray(
        String method,
        String key,
        char[] decrypted,
        int option
    ) throws ServletException {
        try {
            char [] c = Cipher.encryptFromCharArray(method, key, decrypted, option);
            return c;
        } catch (Throwable t) {
            throw new ServletException(
                    "Error encountered in EbppxCipher.encryptFromCharArray: " +
                        t.toString(),
                    t
                    );
        }
    }*/

    /**
     * Decrypt some text.
     *
     * @param   method The desired cipher algorithm.
     *          The recommended value is "3DES/PCBC/PKCS5".
     *
     * @param   key The secret encryption key.
     *          For the recommended cipher method,
     *          a 24-character key is required.
     *
     * @param   encrypted The cipher-text to be decrypted
     *
     * @param   option Pass <b>1</b> to indicate an integrity header is present
     *          in the input cipher-text;
     *          pass <b>0</b> if no header is present.
     *
     * @return  The decrypted result.
     */
    public static String decryptToString(
        String method,
        String key,
        String encrypted,
        int option
    ) throws ServletException {
        if (encrypted == null) {
            return "";
        }

        String cipherText = encrypted;
        try {
            System.out.println("METHOD  = ["+ method +"]");
            System.out.println("KEY     = ["+ key +"]");
            System.out.println("INPUT   = ["+ cipherText +"]");
            System.out.println("OPTION  = ["+ option +"]");
            //String s = Cipher.decryptToString(method, key, cipherText, 0);
            String s = EncryptionHelper.decrypt(cipherText, key);
            System.out.println("DECRYPT = [" + s +"]");
            return s;
        } catch (Throwable t) {
            throw new ServletException(
                    "Error encountered in EbppxCipher.decryptToString: " +
                        t.toString(),
                    t
                    );
        }
    }

    /**
     * Decrypt a character array.
     *
     * @param   method The desired cipher algorithm.
     *          The recommended value is "3DES/PCBC/PKCS5".
     *
     * @param   key The secret encryption key.
     *          For the recommended cipher method,
     *          a 24-character key is required.
     *
     * @param   encrypted The cipher-text to be decrypted
     *
     * @param   option Pass <b>1</b> to indicate an integrity header is present
     *          in the input cipher-text;
     *          pass <b>0</b> if no header is present.
     *
     * @return  The decrypted result.
     */
    /*public static char[] decryptToCharArray(
        String method,
        String key,
        char[] encrypted,
        int option
    ) throws ServletException {
        try {
            char[] c = Cipher.decryptToCharArray(method, key, encrypted, option);
            return c;
        } catch (Throwable t) {
            throw new ServletException(
                    "Error encountered in EbppxCipher.decryptToCharArray: " +
                        t.toString(),
                    t
                    );
        }
    }*/
}
